package com.sdleyou.treepro.common.rabbitmq.receiver;


import com.sdleyou.treepro.common.model.dao.BroadcastDO;
import com.sdleyou.treepro.common.rabbitmq.AmqpExchange;
import com.sdleyou.treepro.common.rabbitmq.event.BroadcastMsgEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 广播通知消费者
 *
 * @Author guxiang
 * @Date 2020-03-15
 */
@Component
public class BroadcastReceiver {

    @Autowired
    private List<BroadcastMsgEvent> events;

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = AmqpExchange.BROADCAST_MESSAGE + "_QUEUE"),
            exchange = @Exchange(value = AmqpExchange.BROADCAST_MESSAGE, type = ExchangeTypes.FANOUT)
    ))
    public void broadcastInit(BroadcastDO broadcastDO) {
        if (events != null && events.size() > 0) {
            for (BroadcastMsgEvent event : events) {
                try {
                    event.recordBroadcast(broadcastDO);
                } catch (Exception e) {
                    logger.info("||=====broadcastInit=====|| 消费者错误: {}", e.getMessage());
                }
            }
        }
    }

}
